<?php
namespace Common\Model;

/**
 * 价格变化记录表
 * @modify zhangzhen 2017-03-02 创建
 */

Class PriceRecordModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'tb_price_record';
    protected function _before_insert(&$data, $options) {
        $data['creator'] = getUserId();
    }
    protected function _before_update(&$data, $options) {
    }
    /**
     * 添加记录
     * @modify zhangzhen 2017-03-02 创建
     * @param  int   $hotel          酒店ID
     * @param  int   $roomType       房型ID
     * @param  int   $detail         操作人ID
     * @param  int   $startdate      起始日期
     * @param  int   $enddate        结束日期
     * @param  int   $pricetype      价格类型
     * @param  int   $from           来源
     * @return id               执行结果
     */
    public function addInfo($hotel, $roomType, $detail, $startdate, $enddate, $pricetype, $from) {
        $data = [
            'hotelentity_id' => $hotel,
            'roomtype_id' => $roomType,
            'detail' => $detail,
            'startdate' => $startdate, 
            'enddate' => empty($enddate)? 0 : $enddate,
            'pricetype' => $pricetype,
            'from' => empty($from)? 0 : $from,
        ];
        return $this->add($data);
    }

    /**
     * 改价记录
     * @modify zhangzhen 2017-03-02     创建
     * @param  mixin   $orderRecordId 父订单修改记录id(s)
     * @return array                  修改记录数组
     */
    public function getInfo() {
        $option = array();
        $data = $this->select();

        return empty($data) ? array() : $data;
    }

    /**
     *  根据条件获取改价记录
     * @modify zhangzhen 2017-03-02  创建
     * @param  int   $hotel          酒店ID
     * @param  int   $roomtype       房型ID
     * @param  int   $creator        操作人ID
     * @param  int   $pricetype      价格类型ID
     * @param  int   $sale           销售时间还是操作时间ID
     * @param  int   $startDate      起始日期
     * @param  int   $endDate        结束日期
     * @param  int   $pageIndex      页码
     * @return array                 数组记录
     */
    
     public function getInfoByCondition($hotel, $roomtype, $creator, $pricetype, $sale, $startDate, $endDate, $pageIndex, $pageSize){
        $option = ['hotelentity_id'=>$hotel];
        if(!empty($roomtype)){
            $option['roomtype_id'] = $roomtype;
        }
        if(!empty($creator)){
            $option['creator'] = $creator;
        }
        if(!empty($pricetype)){
            $option['pricetype'] = $pricetype;
        }
        if(!empty($sale)){
            $option['startdate'] = ['between',[strtotime($startDate), strtotime($endDate)]];
        }else{
             $option['unix_timestamp(createtime)'] = ['between',[strtotime($startDate), strtotime($endDate." 23:59:59")]];
        }
        $data = $this->where($option)->field("*,from_unixtime(startdate,'%Y-%m-%d') as startdate,from_unixtime(enddate,'%Y-%m-%d') as enddate")->limit(($pageIndex-1)*$pageSize, $pageSize)->order('id desc')->select();
        return empty($data) ? ['data'=>[]] : ['data'=>$data,'count'=>count($data)];
     }
    /**
     * group by roomtype
     * @modify zhangzhen 2017-03-02     创建
     * @param  int   $hotel           酒店ID
     * @return array                  修改记录数组
     */
    public function GroupByRoomType($hotel) {
        $option = ['record.hotelentity_id'=>$hotel];
        $data = $this->alias('record')
                    ->field('roomtype_id,name,isdelete')
                    ->join('tb_room_type as type ON type.id = record.roomtype_id','left')
                    ->where($option)
                    ->group('roomtype_id')
                    ->select();

        return empty($data) ? array() : $data;
    }
    /**
     * group by creator
     * @modify zhangzhen 2017-03-02     创建
     * @param  int   $hotel           酒店ID
     * @return array                  修改记录数组
     */
    public function GroupByCreator($hotel) {
        $option = ['hotelentity_id'=>$hotel];
        $data = $this->alias('record')
                    ->field('creator,name')
                    ->join('tb_admin_login as admin ON admin.id = record.creator','left')
                    ->where($option)
                    ->group('creator')
                    ->select();

        return empty($data) ? array() : $data;
    }
}
